package org.example.demo21.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author guocong
 * @since 2024/4/22 14:09
 */
@Configuration
public class RabbitDelayConfig {


    /** 延迟交换机 -- 类型 */
    public static final String DELAY_EXCHANGE_TYPE = "x-delayed-type";

    public static final String DELAY_TYPE = "x-delayed-message";


    //    队列
    public static final String DELAYED_QUEUE_NAME = "delayed.queue";
    //    交换机
    public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    //  routingKey
    public static final String DELAYED_ROUTING_KEY = "delayed.key";


    /**
     * 自定义延迟交换机
     *
     * @return {@link CustomExchange}
     */
    @Bean
    public CustomExchange delayedExchange() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put(DELAY_EXCHANGE_TYPE, "direct");
        return new CustomExchange(DELAYED_EXCHANGE_NAME, DELAY_TYPE, true, false, arguments);
    }


    /**
     * 延迟队列
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue delayedQueue() {
        return new Queue(DELAYED_QUEUE_NAME);
    }


    /**
     * 队列绑定
     *
     * @return {@link Binding}
     */
    @Bean
    public Binding delayedBinding() {
        return BindingBuilder.bind(delayedQueue()).to(delayedExchange()).with(DELAYED_ROUTING_KEY).noargs();
    }


}
